package org.android.kumoh_ing;

import java.net.URL;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class FeedListAdapter2 extends BaseAdapter {

	public static String TAG = "FeedListAdapter";
	private String name ;
	private String pictureUrl ;
	private String date ;
	private String message ;
	private FeedItem curItem;
	private Context mContext;
	private String address;
	
	private ArrayList<String> mList = new ArrayList<String>(); 

	private ArrayList<FeedItem> mItems = new ArrayList<FeedItem>();

	public FeedListAdapter2(Context context) {
		mContext = context;
	}

	public void setListItems(ArrayList<FeedItem> list) {
		mItems = list;
	}

	public void clear() {
		if (mItems != null) {
			mItems.clear();
		}
	}

	public void addItem(FeedItem aItem) {
		mItems.add(aItem);
	}

	public int getCount() {
		return mItems.size();
	}

	public Object getItem(int position) {
		return mItems.get(position);
	}

	public boolean areAllItemsSelectable() {
		return false;
	}

	public boolean isSelectable(int position) {
		return true;
	}

	public long getItemId(int position) {
		return position;
	}

	public ArrayList<String> getCheckedIds(){
		  mList.clear();

		 for(int i=0;i<getCount();i++){
		   if(mItems.get(i).getState()==true){
		    //////TODO
		    
		    mList.add(mItems.get(i).getId());
		   }
		  }
		 
		  return mList;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		FeedItemView2 itemView;
		if (convertView == null) {
			itemView = new FeedItemView2(mContext);
		} else {
			itemView = (FeedItemView2) convertView;
		}

		try {
			curItem = mItems.get(position);

			name = curItem.getName();
			pictureUrl = curItem.getPicture();
			date = curItem.getDate();
			message = curItem.getMessage();
			address = curItem.getAddress();
			
			itemView.setText(0, name);
			itemView.setText(1, date);
			itemView.setText(2, message);
			itemView.setText(3, address);

			if (pictureUrl != null) {
				Bitmap curBitmap = BitmapFactory.decodeStream(new URL(pictureUrl).openStream());
				itemView.setIcon(curBitmap);
			} else {
				
				itemView.setIcon(facebookvalue.BasicPicture);
			}

		} catch(Exception ex) {
			ex.printStackTrace();
		}

		return itemView;
	}

}
